package br.com.lucasddaniel.inbuzios.activities;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ListView;
import br.com.lucasddaniel.inbuzios.Controller;
import br.com.lucasddaniel.inbuzios.R;
import br.com.lucasddaniel.inbuzios.adapter.PacotesAdapter;
import br.com.lucasddaniel.inbuzios.bean.PacoteArquivoBean;
import br.com.lucasddaniel.inbuzios.dao.ExcessaoBanco;

public class ListarPacotesActivity extends Activity {
    private ListView m_listView_pacotes;
    private List<PacoteArquivoBean> listaPacotes;
    private List<PacoteArquivoBean> listaPacotesSelecionados;
    private PacotesAdapter m_adapter_pacotes;
    private Button m_btn_comprar;
    private int idTuristaComprador = 0;
    private final Handler mHandler = new Handler();

    // private FilterTextWatcher filterTextWatcher;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_listar_pacotes);
        idTuristaComprador = getIntent().getIntExtra("idTuristaComprador", 0);
        // searchBox = ( EditText ) findViewById( R.id_activity_listar_pacotes.edttext_search );
        // searchBox.setInputType( searchBox.getInputType()
        // | EditorInfo.TYPE_TEXT_FLAG_NO_SUGGESTIONS );

        listaPacotes = new ArrayList<PacoteArquivoBean>();
        listaPacotesSelecionados = new ArrayList<PacoteArquivoBean>();
        try {
            listaPacotes = Controller.getInstance().listarPacotesArquivo();
        } catch (ExcessaoBanco e) {
            e.printStackTrace();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
            notificarUsuario("", e.getMessage());
        } catch (IOException e) {
            e.printStackTrace();
        }
        initComponents();
        m_btn_comprar = (Button) findViewById(R.id_activity_listar_pacotes.btn_avancar);
        m_btn_comprar.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                if (!listaPacotesSelecionados.isEmpty()) {
                    ArrayList<String> listaIdsPacotesComprados = new ArrayList<String>();
                    for (PacoteArquivoBean pac : listaPacotesSelecionados) {
                        listaIdsPacotesComprados.add(pac.getId() + "");
                    }
                    Intent i;
                    if (idTuristaComprador > 0) {
                    	i = new Intent(getApplicationContext(), GeracaoImpressaoPasseioActivity.class);
                        i.putStringArrayListExtra("listaIdsPacotesComprados", listaIdsPacotesComprados);
                        i.putExtra("idTuristaComprador", idTuristaComprador);
                    } else {
                    	i = new Intent(getApplicationContext(), ListarClientesCompraActivity.class);
                    }
                    i.putStringArrayListExtra("listaIdsPacotesComprados", listaIdsPacotesComprados);
                    startActivity(i);
                } else {
                    notificarUsuario("", getApplicationContext().getString(R.string.activity_listar_pacotes_toast));
                }
            }
        });

        // filterTextWatcher = new FilterTextWatcher( m_adapter_pacotes, searchBox, R.string.activity_listar_pacotes_contratados_hint );
        // searchBox.addTextChangedListener( filterTextWatcher );
    }

    private void initComponents() {
        m_listView_pacotes = (ListView) findViewById(R.id_activity_listar_pacotes.listview);
        m_adapter_pacotes = new PacotesAdapter(ListarPacotesActivity.this, R.layout.row_pacotes, listaPacotes);
        m_listView_pacotes.setAdapter(m_adapter_pacotes);
        m_listView_pacotes.setOnItemClickListener(new OnItemClickListener() {
            public void onItemClick(AdapterView<?> arg0, View v, int position, long arg3) {
                ImageView check = (ImageView) v.findViewById(R.id_row_pacotes.checkbox);
                if (!listaPacotesSelecionados.contains(listaPacotes.get(position))) {
                    listaPacotesSelecionados.add(listaPacotes.get(position));
                    check.setVisibility(View.VISIBLE);
                } else {
                    check.setVisibility(View.INVISIBLE);
                    listaPacotesSelecionados.remove(listaPacotes.get(position));
                }
            }
        });
    }

    protected void notificarUsuario(final String titulo, final String message) {
        mHandler.post(new Runnable() {
            public void run() {
                new AlertDialog.Builder(ListarPacotesActivity.this).setTitle(titulo).setMessage(message).create().show();
            }
        });
    }

    // @Override
    // protected void onDestroy() {
    // super.onDestroy();
    // searchBox.removeTextChangedListener( filterTextWatcher );
    // }
}
